package com.hulizuofang.wol.chat.client.view;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.hulizuofang.wol.chat.client.data.ChatUser;
import com.hulizuofang.wol.chat.client.gin.ClientI18nMessages;
import com.hulizuofang.wol.chat.client.gin.ComponentFactory;
import com.hulizuofang.wol.chat.client.presenter.WhoisOnlinePagePresenter.PageView;
import com.hulizuofang.wol.chat.client.ui.chat.ChatPanel;
import com.hulizuofang.wol.chat.client.ui.chat.WhoisOnlinePanel;

public class WhoisOnlinePageViewImpl extends ViewImpl implements PageView {
	private AbsolutePanel container;
	private final WhoisOnlinePanel whoisOnlinePanel;
	private SimplePanel displayLogin;
	private final ComponentFactory componentFactory;
	private final Map<String, ChatPanel> opennedChannel;
	private ChatUser user;

	@Inject
	public WhoisOnlinePageViewImpl(ComponentFactory componentFactory, ClientI18nMessages i18n, WhoisOnlinePanel whoisOnlinePanel) {
		this.componentFactory = componentFactory;
		this.whoisOnlinePanel = whoisOnlinePanel;
		container = new AbsolutePanel();
		displayLogin = new DecoratorPanel();
		container.add(displayLogin);
		container.add(whoisOnlinePanel);
		opennedChannel = new HashMap<String, ChatPanel>();
	}

	@Override
	public Widget asWidget() {
		return container;
	}

	@Override
	public void refreshOnlineUser(Set<ChatUser> users) {
		whoisOnlinePanel.refreshOnlineUser(users);
	}

	@Override
	public void showUser(ChatUser user) {
		this.user = user;
		displayLogin.setWidget(componentFactory.newChatUserPanel(user));
	}
	
	@Override
	public void addMyMessage(ChatUser recipient, String message) {
		ChatPanel chatPanel = addIfNotExistsChatPanel(recipient);
		chatPanel.addMyMessage(message);
	}

	@Override
	public void addOtherMessage(ChatUser sender, String message) {
		ChatPanel chatPanel = addIfNotExistsChatPanel(sender);
		chatPanel.addOtherMessage(message);
	}
	
	@Override
	public void addErrorMessage(ChatUser recipient, String message, String error) {
		ChatPanel chatPanel = addIfNotExistsChatPanel(recipient);
		chatPanel.addErrorMessage(message,error);
	}
	
	@Override
	public void addUndeliveredMessage(ChatUser recipient, String message) {
		ChatPanel chatPanel = addIfNotExistsChatPanel(recipient);
		chatPanel.addUndeliveredMessage(message);
	}

	@Override
	public void openChat(ChatUser sender) {
		addIfNotExistsChatPanel(sender);
	}

	private ChatPanel addIfNotExistsChatPanel(ChatUser sender) {
		ChatPanel chatPanel = opennedChannel.get(sender.getLogin());
		if (chatPanel == null) {
			chatPanel = componentFactory.newChatPanel(user.getLogin(), sender);
			opennedChannel.put(sender.getLogin(), chatPanel);
			container.add(chatPanel);
		}
		return chatPanel;
	}
}
